import requests,json
from time import sleep
from datetime import date,timedelta
from pprint import pprint
InstrumentDict={'list':[]}
sess=requests.Session()

sess.headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
}
############################################################################
#上海
SHFE_url='https://www.shfe.com.cn/data/indexdelaymarket.dat'
res=sess.get(SHFE_url)
for item in res.json()['indexdelaymarket']:
    InstrumentDict['list'].append(item['contractname'])
    print('SHFE',item['contractname'])
print('上海抓取成功！')
#############################################################################
#郑州    http://www.czce.com.cn/cn/DFSStaticFiles/Future/2024/20240227/FutureDataDaily.txt
date_str=date.strftime(date.today(),'%Y%m%d')
while 1:
    CZCE_url='http://www.czce.com.cn/cn/DFSStaticFiles/Future/2024/%s/FutureDataDaily.txt'%date_str
    res=sess.get(CZCE_url)
    if len(res.text)>500:break
    print('郑商所，'+date_str+'不开市')
    date_str=date.strftime(date.today()-timedelta(days=1),'%Y%m%d')

ls=res.text.split('\n')
for line in ls:
    if len(line)>0 and 'A' <= line[0] <= 'Z':InstrumentDict['list'].append(line[:5])
    print('CZCE',line[:5])
print('郑州抓取成功！')
###############################################################################
#大连    http://www.dce.com.cn/publicweb/quotesdata/exportDayQuotesChData.html
i=0
while 1:
    DCE_url='http://www.dce.com.cn/publicweb/quotesdata/exportDayQuotesChData.html'
    today=date.today()-timedelta(days=i)
    datas={
        'dayQuotes.variety': 'all',
        'dayQuotes.trade_type': 0,
        'year': today.year,
        'month': today.month-1,#不知道为啥要减一
        'day': today.day,
        'exportFlag': 'txt',
    }
    res=sess.post(DCE_url,data=datas)
    if len(res.text)>500:break
    print('大连商品商所，'+str(today.day)+'日暂无数据')
    print(res.text)
    i+=1
    sleep(2)

# pprint(res.text)
ls=res.text.split('\t')
for line in ls:
    if len(line) in [5,6] and'a' <= line[0] <= 'z':
        # print(line)
        InstrumentDict['list'].append(line)
        print('DCE',line)
print('大连抓取成功！')
#广期所
        

with open('InstrumentIDList.json','w',encoding='utf8') as f:
    json.dump(InstrumentDict,f)
